package com.yjf.question.login;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;
import com.yjf.question.config.WxConfig;
import com.yjf.question.utils.ResultUtils;
import com.yjf.question.utils.ResultVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/wxapi/login")
public class WxLoginController {

    @Resource
    private WxConfig wxConfig;


    @RequestMapping("/wxLogin")
    public ResultVo wxLogin(@RequestParam("code") String code){

        Map<String,String> map = new HashMap<>();
        map.put("appid", wxConfig.getAppid());
        map.put("secret", wxConfig.getSecret());
        map.put("js_code",code);
        map.put("grant_type", wxConfig.getGrantType());
        String body = HttpRequest.get(wxConfig.getUrl()).form(map).body();
        JSONObject object  = JSON.parseObject(body);
        System.out.println(object);
        if (!object.get("openid").equals("")){
            return ResultUtils.success("获取成功",object);
        }

        return ResultUtils.error("获取失败",object);

    }


}
